﻿/*jslint devel: true, bitwise: false, undef: false, browser: true, continue: false, debug: false, eqeq: false, es5: false, type: false, evil: false, vars: false, forin: false, white: true, newcap: false, nomen: true, plusplus: false, regexp: true, sloppy: true */
/*globals $, jQuery, FB, TDFriendSelector */
var user;
window.fbAsyncInit = function () {

    FB.init({ appId: '183163201853200', status: true, cookie: false, xfbml: false, oauth: true });
    FB.Event.subscribe('edge.create', function (response) {
        var test = response;
    });
    $(document).ready(function () {
        $('#sharebutton').click(function (e) {
            jQuery.ajax({
                type: "POST",
                url: 'SSEHandler.ashx',
                async: true,
                data: {
                    method: 'addpoint'
                },
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (d) {
                    func();
                }
            });
        });
        FB.XFBML.parse();
        $("#btnLogin").click(function (e) {
            e.preventDefault();
            FB.login(function (response) {
                if (response.status === 'connected') {
                    jQuery('#connectwithFB').hide();
                    FB.api('/me', {
                        fields: 'name, id,email,gender,location,first_name,last_name,link,picture'
                    },
                    function (response) {
                        if (!response.error) {
                            response;
                            $.ajax({
                                url: 'SSEHandler.ashx',
                                type: 'POST',
                                dataType: "json",
                                data: {
                                    method: 'saveregister',
                                    username: response.email,
                                    email: response.email,
                                    firstname: response.first_name,
                                    lastname: response.last_name,
                                    sex: response.gender
                                },
                                success: function (data) {

                                    if (data.result == false) {
                                        window.location.href = 'member-home.html';
                                        return false;
                                    }
                                    else {
                                        window.location.href = 'member-home.html';
                                        return false;

                                    }
                                }
                            });
                            //Update display of user name and picture
                        }
                    });
                }
            }, { scope: 'email,user_likes' });
        });
        $("#btnLogout").click(function (e) {
            e.preventDefault();
            FB.logout();
            $("#login-status").html("Not logged in");
        });

        //myfunction(function () {
        FB.getLoginStatus(function (response) {
            if (response.authResponse) {
                FB.api('/me', {
                    fields: 'name, id,email,gender,location,first_name,last_name,link,picture'
                }, function (response2) {
                    if (!response2.error) {
                        user = response2;
                        jQuery('#memberPicture').attr('src', response2.picture.data.url);
                        jQuery('#memberName').text(response2.name);

                        $.ajax({
                            url: 'SSEHandler.ashx',
                            type: 'POST',
                            dataType: "json",
                            data: {
                                method: 'fblogin',
                                username: response2.email,
                                accesstoken: response.authResponse.accessToken,
                                urlAuthen: window.location.href
                            },
                            success: function (d) {
                                if (d == false) {
                                    if (window.location.href.indexOf("login.html") == -1) {
                                        window.location.href = 'login.html';
                                    }
                                }
                                if (d) {
                                    getcurrent(response2.email);
                                    getNoti();
                                    if (window.location.href.indexOf("login.html") !== -1) {
                                        window.location.href = 'member-home.html';
                                    }
                                }
                            },
                            error: function (d) {
                                if (d.responseText == 'false') {
                                    if (window.location.href.indexOf("login.html") == -1) {
                                        window.location.href = 'login.html';
                                    }
                                }
                                if (d.responseText == 'true') {
                                    getcurrent(response2.email);
                                    getNoti();
                                    if (window.location.href.indexOf("login.html") !== -1) {
                                        window.location.href = 'member-home.html';
                                    }
                                }

                            }
                        });
                    }
                });
            } else {
                myfunction();
            }
        });
        //});

    });
};


var lastNoti;
function getNoti() {
    $.ajax({
        url: 'SSEHandler.ashx',
        type: 'POST',
        dataType: "json",
        data: {
            method: 'getnotify'
        },
        success: function (data) {
            if (data.noti != undefined && data.noti.length > 0) {
                var message = '';
                lastNoti = data.noti[data.noti.length - 1];
                $('#notistar').text(lastNoti.star);
                $('#myModal').foundation('reveal', 'open');
                $('#popupclose').click(function () {
                    updateNoti(lastNoti.id);
                    $('#myModal').foundation('reveal', 'close');
                });
            }
        },
        error: function (d) {
            if (d.responseText == 'False') {
                if (window.location.href.indexOf("login.html") == -1) {
                    window.location.href = 'login.html';
                }
            }
            if (d.responseText == 'True') {
                if (window.location.href.indexOf("login.html") !== -1) {
                    window.location.href = 'member-home.html';
                }
            }

        }
    });

}
function getcurrent(email) {
    $.ajax({
        url: '/ifs/ifs.ashx',
        async: false,
        type: 'GET',
        dataType: "json",
        data: { metdhodName: 'getcurrent', username: email },
        success: function (json) {
            if (json == null) {
                return false;
            }
            for (var i = 0; i < json.length; i++) {
                //alert(json[i].Curent_Star);
                //$('#memberName').text(json[i].Email);
                $('#point').text(json[i].Curent_Point);
                $('#star').text(json[i].Curent_Star);
            }
        }
    });
}
function updateNoti(notiID) {
    $.ajax({
        url: 'SSEHandler.ashx',
        type: 'POST',
        dataType: "json",
        data: {
            'method': 'updatenotify',
            'notifyid': notiID
        },
        success: function (data) {
            if (data != undefined) {
                var message = '';
            }
        },
        error: function (d) {
            //alert(d);
        }
    });

}
function myfunction(func) {

    jQuery.ajax({
        type: "GET",
        url: 'SSEHandler.ashx',
        async: true,
        data: {
            method: 'authenticate',
            urlAuthen: window.location.href
        },
        dataType: "json",
        success: function (data) {
            alert(data);
            func();
        },
        error: function (d) {
            if (d.responseText == 'False') {
                if (window.location.href.indexOf("login.html") == -1) {
                    window.location.href = 'login.html';
                }
            }

        }
    });
}

function requestCallback(data) {
    var d = data;

}
(function () {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

